package com.itany.nmms.service.proxy;

import com.itany.nmms.entity.ProductType;
import com.itany.nmms.exception.ProductTypeExistException;
import com.itany.nmms.exception.RequestParameterErrorException;
import com.itany.nmms.exception.ServiceException;
import com.itany.nmms.factory.ObjectFactory;
import com.itany.nmms.service.ProductTypeService;
import com.itany.nmms.tran.TransactionManager;

import java.util.List;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/4/28 9:11
 * Version:1.0-SNAPSHOT
 * Description:
 */
public class ProductTypeServiceProxy implements ProductTypeService {

    @Override
    public void addType(String name) throws RequestParameterErrorException, ProductTypeExistException, ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ProductTypeService typeService = (ProductTypeService) ObjectFactory.getObject("typeServiceTarget");
        try {
            tran.begin();
            typeService.addType(name);
            tran.commit();
        } catch (RequestParameterErrorException e) {
            tran.rollback();
            throw e;
        } catch (ProductTypeExistException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public List<ProductType> findAll() throws ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ProductTypeService typeService = (ProductTypeService) ObjectFactory.getObject("typeServiceTarget");

        try {
            tran.begin();
            List<ProductType> types = typeService.findAll();
            tran.commit();
            return types;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public List<ProductType> findEnable() throws ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ProductTypeService typeService = (ProductTypeService) ObjectFactory.getObject("typeServiceTarget");

        try {
            tran.begin();
            List<ProductType> types = typeService.findEnable();
            tran.commit();
            return types;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }

}
